//
//  XSPlayViewController.m
//  XSDownloadDemo
//
//  Created by 大帅 on 2023/12/22.
//

#import "XSPlayViewController.h"

#import <ZFPlayer/ZFAVPlayerManager.h>

@interface XSPlayViewController ()
/** 播放器依赖视图 */
@property (nonatomic, strong) UIView *tempView;
/** 播放器 */
@property (nonatomic, strong) ZFPlayerController *playView;
/** 播放器控制层 */
@property (nonatomic, strong) ZFPlayerControlView *playControlView;
/** 判断pop返回 */
@property (nonatomic, assign) NSInteger vcCount;


@property (strong, nonatomic) AVPlayer *player;

@property (strong, nonatomic) AVPlayerItem *playerItem;

@property (strong, nonatomic) AVPlayerLayer *playerLayer;

@end

@implementation XSPlayViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.vcCount = self.navigationController.viewControllers.count;
    self.playView.viewControllerDisappear = NO;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.playView.viewControllerDisappear = YES;
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    // 页面pop返回
    if (self.vcCount > self.navigationController.viewControllers.count) {
        [self.playView stop];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"player";
    self.view.backgroundColor = [UIColor.orangeColor colorWithAlphaComponent:1];
    
    self.vcCount = self.navigationController.viewControllers.count;
    
    self.tempView = [[UIView alloc] init];
    self.tempView.backgroundColor = UIColor.blackColor;
    [self.view addSubview:self.tempView];
    [self.tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(GK_STATUSBAR_NAVBAR_HEIGHT);
        make.height.mas_equalTo(self.view.frame.size.width / 16 * 9);
    }];
    
    // 播放器初始化
    ZFAVPlayerManager *manager = [[ZFAVPlayerManager alloc] init];
    self.playView = [ZFPlayerController playerWithPlayerManager:manager containerView:self.tempView];
    self.playView.controlView = self.playControlView;
    
    NSString *str = self.isM3u8 ? self.modelM3u8.locaM3u8lUrl : self.model.localUrl;
    NSString *playStr = [self stringChangeChineseTransformWithUrlString: str];
    NSLog(@"播放地址：%@", playStr);
    if (self.isM3u8) {
        self.playView.assetURL = [NSURL URLWithString:playStr];
    } else {
        self.playView.assetURL = [NSURL fileURLWithPath:playStr];
    }
    
}

// 检查字符串是否为空
- (BOOL)trimEmpty:(NSString *)str {
    if (!str) {
        return YES;
    }
    if ([str isKindOfClass:[NSNull class]]) {
        return YES;
    }
    NSString *trimStr = [[NSString stringWithFormat:@"%@",str] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if([trimStr isEqualToString:@"(null)"] || [trimStr isEqualToString:@"null"]) {
        return YES;
    }
    return (trimStr == nil || trimStr.length <= 0);
}


// url汉字处理
- (NSString *)stringChangeChineseTransformWithUrlString:(NSString*)string{
    return [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

// 控制层
- (ZFPlayerControlView *)playControlView {
    if (!_playControlView) {
        _playControlView = [[ZFPlayerControlView alloc] init];
        _playControlView.fastViewAnimated = YES;
        _playControlView.autoHiddenTimeInterval = 3;
        _playControlView.prepareShowLoading = YES;
        _playControlView.prepareShowControlView = NO;
        _playControlView.showCustomStatusBar = YES;
    }
    return _playControlView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
